package com.fili.editor;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Fili
 * Date: Mar 11, 2007
 * Time: 1:31:39 PM
 */
public class EditPanel extends JPanel implements MouseListener, MouseMotionListener, Scrollable
{
    private ArrayList<BezierPath> paths;

    private int buttonDown;

    public EditPanel()
    {
        paths = new ArrayList<BezierPath>();
        paths.add(new BezierPath());

        addMouseListener(this);
        addMouseMotionListener(this);
    }


    protected void paintComponent(Graphics graphics)
    {
        Graphics2D g = (Graphics2D) graphics;
        g.setColor(Color.white);
        g.fillRect(0, 0, getWidth(), getHeight());
        for (BezierPath path : paths)
            path.render(g);
    }

    public void mouseClicked(MouseEvent e)
    {
    }

    public void mousePressed(MouseEvent e)
    {
        buttonDown = e.getButton();
        for (BezierPath path : paths)
        {
            path.select(e.getX(), e.getY());
        }

        repaint();
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
    }

    public void mouseDragged(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();

        if (buttonDown == MouseEvent.BUTTON1)
        {
            x = (x / 10) * 10;
            y = (y / 10) * 10;
        }
        for (BezierPath path : paths)
        {
            path.drag(x, y);
        }

        repaint();
    }

    public void mouseMoved(MouseEvent e)
    {
    }


    public Dimension getPreferredSize()
    {
        return new Dimension(800, 600);
    }

    public Dimension getPreferredScrollableViewportSize()
    {
        return getPreferredSize();
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
    {
        return 10;
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
    {
        return 50;
    }

    public boolean getScrollableTracksViewportWidth()
    {
        return false;
    }

    public boolean getScrollableTracksViewportHeight()
    {
        return false;
    }
}
